package com.zhai.boot.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @package: com.zhai.boot.util
 * @project-name: zhai-boot
 * @description: List工具类
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-7-10 17:39
 */
public class ListUtil {

    /**
     * 拆分List
     * @param: list 被拆分的List
     *         count 每个List的元素个数
     */
    public static <T> List<List<T>> split(List<T> list, int count) {
        if (list == null || list.size() <= 0 || count < 1) {
            return null;
        }
        List<List<T>> resultList = new ArrayList<>();
        Integer size = list.size();
        //判断是否有必要分批
        if (count >= size) {
            resultList.add(list);
            return resultList;
        }
        //分批数
        int part = size / count;
        for (int i = 0; i < part; i++) {
            //分割
            List<T> partList = list.subList(i * count, count * (i + 1));
            resultList.add(partList);
        }
        //处理剩下的
        int over = size % count;
        if (over != 0) {
            List<T> partList = list.subList(part * count, list.size());
            resultList.add(partList);
        }
        return resultList;
    }

    /**
     * list 为空
     */
    public static <T> Boolean isNull(List<T> list) {
       return list == null || list.size() == 0;
    }

    /**
     * list 不为空
     */
    public static <T> Boolean isNotNull(List<T> list) {
        return list != null && !list.isEmpty();
    }

    /**
     * 比较集合中存在某个比较对象
     * @author: 翟凤玺
     */
    public static <T> boolean isEquals(List<T> list,Object compare) {
        return list.contains(compare);
    }

    /**
     * 比较集合中不存在某个比较对象
     */
    public static <T> boolean isNotEquals(List<T> list,Object compare) {
        return !list.contains(compare);
    }

    /**
     * list 转 ArrayList
     */
    public static <T> List<T> toArryList(List<T> list) {
        return new ArrayList<>(list);
    }

    /**
     * 字符串集合 按指定标识符 拼接成字符串
     */
    public static String join(List<String> list, String separator) {
       return StringUtils.join(list, separator);
    }

    /**
     * List集合去重
     * @param list 集合
     * @return 去重后集合
     */
    public static <T> List<T> removeDup(List<T> list) {
        return new ArrayList<>(new HashSet<>(list));
    }

    public static void main(String[] args) {
        String A = "15001";
        String B = "15002";
        String C = "15003";
        List<String> result = ListUtil.toArryList(Arrays.asList(A, B, C));
        System.out.println(ListUtil.join(result, ","));
        List<String> list = Arrays.asList(new String[]{"Tom", "Tom", "Bob", "Jane"});
        List<String> removeDup = ListUtil.removeDup(list);
        System.out.println(JSONUtil.toStr(removeDup));
        System.out.println(ListUtil.isNull(list));
        System.out.println(ListUtil.isNotNull(list));
    }
}
